<?php

namespace App\Repository\Repositories;

use App\Models\IntegralRecord;
use App\Models\StoreCoupon;
use App\Models\StoreProduct;
use App\Models\User;
use App\Repository\RepositoryInterface\IntegralInterface;
use Illuminate\Validation\ValidationException;

class IntegralRepositories implements IntegralInterface
{
    /**
     * @return mixed
     * 查询积分可兑换商品
     */
    public function isIntegral()
    {
        return StoreProduct::where('is_exchange',1)->get();
    }

    /**
     * @return array
     * 查询积分可兑换优惠券
     */
    public function integralCoupon()
    {
        try {
            $coupon = StoreCoupon::get();
            foreach ($coupon as $value){
                if ($value->integral == 0){
                    continue;
                }
                $coupon_all[] = $value;
            }
            return $coupon_all;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param
     * $id 商品id
     * @return float|int
     * 兑换商品所需积分
     */
    public function integralExchangeGoods($id)
    {
        try {
            $goods_data = StoreProduct::where('id',$id)->first();
            $integral = round($goods_data->price*20/100)*100;//兑换商品所需积分
            return $integral;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param
     * $user_id 用户id
     * @return mixed
     * 查询用户剩余积分
     */
    public function userIntegralBalance($user_id)
    {
        try {
            $user_data = User::where('uid',$user_id)->first();
//            return $user_data;
            $user_integral_balance = $user_data->integral;
            return $user_integral_balance;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    public function userIntegralBuckle($user_id,$integral_exchange_goods,$user_integral_balance)
    {
        try {
            //用户消耗积分
            $results_buckle = User::where('uid',$user_id)->update(['integral' =>$user_integral_balance-$integral_exchange_goods]);
            if ($results_buckle){
                return true;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @param $integral_end
     * @param $integral_reduce
     * @return void
     * 用户积分变动记录
     */
    public function integralFluctuationRecording($user_id,$integral_end,$integral_reduce){
        try {
            $integral_fluctuation_recording = IntegralRecord::insert([
                'uid' => $user_id,
                'integral_end' => $integral_end,
                'integral_reduce' => $integral_reduce,
                'add_time' => time()
            ]);
            if ($integral_fluctuation_recording){
                return true;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param $uid
     * @return mixed
     * 积分流动记录查询
     */
    public function integralRecordingQuery($uid){
        try {
            $integral_record = IntegralRecord::where('uid',$uid)->get();
            return $integral_record;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param $uid
     * @return mixed
     * 积分流动记录删除
     */
    public function integralRecordingDelete($Recording_id){
        try {
            $integral_record = IntegralRecord::where('id',$Recording_id)->delete();
            if ($integral_record){
                return '删除成功';
            }
            return '删除失败';
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }
}
